$(function(){
	loadOrderList();
	selectOrderList(false);
});

/**
 * 加载订单列表
 */
function loadOrderList(){
	$('#orderlist_dategrid').datagrid({
		loadMsg : "数据加载中",
		title:"订单列表",
		border:true,
		fit : true,
		fitColumns : true,
		pagination : true,
		rownumbers : true,
		singleSelect : true,
		columns : [ [
		    {field:	 'checked',checkbox:true},
		    {field : 'orderid',hidden : true},
		    {field : 'deliverfee',hidden : true},
		    {field : 'note',hidden : true},
		    {field : 'producttotalprice',hidden : true},
		    {field : 'buyid',hidden : true},
		    {field : 'deliverId',hidden : true},
		    {field : 'ticket',hidden : true},
			{field : 'username',	title : '会员名',	width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'recipients',	title : '收货人',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'createdate',	title : '提交时间',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'paymentway',title : '支付方式',	width : 100,align : 'center',hidden : false,
				formatter: function(value,row,index){
					if(value == '0'){
						return '线下支付';
					}else if(value == '1'){
						return '银联支付';
					}else if(value == '2'){
						return '支付宝支付';
					}else if(value == '3'){
						return '微信支付';
					}else{
						return '未知';
					}
				},
			},
			{field : 'totalprice',	title : '订单实收款(元)',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'paymentstate',	title : '付款状态',		width : 100,align : 'center',
				formatter: function(value,row,index){
					if(value == 0){
						return '等待买家付款';
					}else if(value == 1){
						return '已支付';
					}else{
						return '未知';
					}
				}
			},
			{field : 'status',	title : '订单状态',		width : 100,align : 'center',
				formatter: function(value,row,index){
					if(value == 0){
						return '未处理';
					}else if(value == 1){
						return '已处理';
					}else{
						return '未知';
					}
				}
			},
			{field : 'xq',title : '详情',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return '<a href="#" style="color:blue;margin-left:3px;" onClick="openOrderDetail('+index+')">详情</a>';
				}
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
				    return '<a href="#" style="color:blue;margin-left:3px;" onClick="deleteOrder('+row.orderid+')">删除</a>';
				}
			}
		] ],
		toolbar : [ {
			text : '删除',
			iconCls : 'icon-cancel',
			handler : function() {
				deleteOrder();
			}		
		}, ]
	});
}

/**
 * 查询订单列表
 * @param haveparams 是否有参数，初试加载传入false，点击查询按钮传入true
 */
function selectOrderList(haveparams){
	var params = {};
	
	if(haveparams){
		var username=$.trim($("#order_list_username").val());          		//商品名称
		var orderid=$.trim($("#order_list_orderid").val());                //商家编码
		var recipients=$.trim($("#order_list_recipients").val());                //商家编码
		var createdatebegin=$.trim($('#order_list_createdatebegin').datetimebox('getValue'));            //添加时间 开始
		var createdateend=$.trim($('#order_list_createdateend').datetimebox('getValue'));            	//添加时间 结束
		var note=$.trim($('#order_list_note').combobox('getValue'));                 		//销售状态
		params = {
			username : username,
			orderid : orderid,
			recipients : recipients,
			createdatebegin : createdatebegin,
			createdateend : createdateend,
			note : note
		};
	}
	$("#orderlist_dategrid").datagrid_pagination($("#orderlist_dategrid"),path+"/order/v1/orderlist",params,"","");
}

/**
 * 查询所有品牌（用于下拉菜单）
 */
/*
function selectOrderBrands(){
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/item/v1/selectitembrand?page=1&rows=10000',
		data:'{}',
		dataType:"json",
		success:function(data){
			$("#list_brandId").combobox({
				valueField : 'id',
				textField : 'brandName',
				data : data.rows
			});
		}
   });
}
*/
/**
 * 得到所选择行的id（int型）
 * @returns {Array}
 */
function getSelectionsIds(){
	var orderList = $("#orderlist_dategrid");
	var sels = orderList.datagrid("getSelections");
	var ids = new Array();
	for(var i in sels){
		ids.push(parseInt(sels[i].id));
	}
	return ids;
}

/**
 * 删除订单
 */
function deleteOrder(id){
	if(id == '' || id == undefined){
		var ids = getSelectionsIds();
		if(ids.length == 0){
			$.messager.alert('提示','未选中订单!');
			return ;
		}
		row = $("#orderlist_dategrid").datagrid("getSelected");
		id = row.orderid;
	}
	$.messager.confirm('确认','确定删除orderid为 '+id+' 的订单吗？',function(r){
	    if (r){
	    	var params = {orderid:id};
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/order/v1/deleteorder',
	    		data:JSON.stringify(params),
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除订单成功!',undefined,function(){
	    					selectOrderList(true);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}

/**
 * 打开订单详情
 * @param orderid
 */
function openOrderDetail(index){
	var rows = $('#orderlist_dategrid').datagrid('getRows');
	var row=rows[index];  // 根据行号获取选中行
	loadOrderDetailList(row.orderid,row.buyid,row.deliverId);
	selectOrderDetail(row.orderid,row.buyid,row.deliverId);
	$('#order_detail_dqdd').text(row.status);
	$('#order_detail_fksj').text(row.createdate);
	
	$('#order_detail_yf').text(row.deliverfee);
	$('#order_detail_gwcxj').text(row.producttotalprice);
	$('#order_detail_ddssk').text(row.totalprice);
	
	$('#order_detail_psfs').text(row.note);
	console.log("row.status===="+row.status);
	$('#orderDetailWindow').window("open");
}

/**
 * 加载订单详情列表
 */
function loadOrderDetailList(orderid,buyid,deliverId){
	$('#orderdetaillist_dategrid').datagrid({
		loadMsg : "数据加载中",
		border:true,
		fit : true,
		fitColumns : true,
		rownumbers : true,
		singleSelect : true,
		columns : [ [
		    {field : 'id',hidden : true},
		    {field : 'productid',hidden : true},
		    {field : 'stylename',hidden : true},
		    {field : 'styleid',hidden : true},
		    {field : 'orderId',hidden : true},
			{field : 'productname',	title : '商品名称',	width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'productprice',	title : '商品单价（元）',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'amount',	title : '购买数量',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	value;
				}
			},
			{field : 'amount',title : '发货数量',	width : 100,align : 'center',hidden : false,
				formatter: function(value,row,index){
					return 	value;
				},
			},
			{field : 'totalprice',	title : '小计(元)',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return 	row.productprice * row.amount;
				}
			},
			{field : 'paymentstate',	title : '状态',		width : 100,align : 'center',
				formatter: function(value,row,index){
					return '订单状态还没写';
				}
			},
			{field : 'cz',title : '操作',	width : 50,align : 'center',hidden : false,
				formatter: function(value,row,index){
				    return '<a href="#" style="color:blue;margin-left:3px;" onClick="deleteOrderDetail('+row.id+','+orderid+','+buyid+','+deliverId+')">彻底删除</a>';
				}
			}
		] ]
	});
}

/**
 * 查询订单详情
 */
function selectOrderDetail(orderid,buyid,deliverId){
	$('#orderdetaillist_dategrid').datagrid("loading");
	var params = {
			orderid:orderid,
			buyid:buyid,
			deliverId:deliverId
		};
	$.ajax({
		type:"post",
		contentType:"application/json; charset=utf-8",
		url:path+'/order/v1/orderdetail',
		data:JSON.stringify(params),
		dataType:"json",
		success:function(data){
			if(data.result == 1){
				$('#orderdetaillist_dategrid').datagrid("loaded");
				if(data.list == undefined){
					$('#orderdetaillist_dategrid').datagrid('loadData', { total: 0, rows: [] }); 
				}else{
					$('#orderdetaillist_dategrid').datagrid('loadData',data.list);
				}
				
				$('#order_detail_hym').text(undefined != data.username && null != data.username ? data.username : '');
				$('#order_detail_zsxm').text(undefined != data.realname && null != data.realname ? data.realname : '');
				$('#order_detail_lxdh').text(undefined != data.deliver_mobile && null != data.deliver_mobile ? data.deliver_mobile : '');
				$('#order_detail_dzyj').text(undefined != data.deliver_email && null != data.deliver_email ? data.deliver_email : '');
				$('#order_detail_shdz').text(undefined != data.deliver_address && null != data.deliver_address ? data.deliver_address : '');
				$('#order_detail_mjly').text(undefined != data.deliver_requirement && null != data.deliver_requirement ? data.deliver_requirement : '');
				
			}else{
				$.messager.alert('提示',data.msg);
			}
		}
   });
}

/**
 * 删除订单详情
 */
function deleteOrderDetail(id,orderid,buyid,deliverId){
	$.messager.confirm('确认','确定删除id为 '+id+' 的商品吗？',function(r){
	    if (r){
	    	$.ajax({
	    		type:"post",
	    		contentType:"application/json; charset=utf-8",
	    		url:path+'/order/v1/deleteorderdetail',
	    		data:id+"",
	    		dataType:"json",
	    		success:function(data){
	    			if(data.result == 1){
	    				$.messager.alert('提示','删除商品成功!',undefined,function(){
	    					selectOrderDetail(orderid,buyid,deliverId);
	    				});
	    			}else{
	    				$.messager.alert('提示',data.msg);
	    			}
	    		}
	       });
	    }
	});
}

